const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet, HttpDelete, HttpPost, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/admin/aged')
class AgedCtrl extends Controller {

    //搜索老人列表
    @HttpGet('/search')
    @ExceptionHandler()
    async searchList() {
        const rule = {
            pn: 'int',
            ps: 'int',
            kw: {type: 'string', allowEmpty: true}
        };
        const query = this.ctx.request.query;
        this.validate(rule, query);
        this.result.data = await this.service.admin.aged.main.searchList(query.pn, query.ps, query.kw, this.ctx.session.town_id);
        return '查询成功'
    }

    //批量删除
    @HttpDelete('/')
    @ExceptionHandler()
    async deleteAged() {
        const rule = {
            aged_id_list: 'array'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.aged.main.deleteAged(this.ctx.request.body.aged_id_list);
        return '删除成功'
    }

    //编辑老人
    @HttpPost('/edit')
    @ExceptionHandler()
    async editAged() {
        const rule = {
            aged_id: 'int',
            name: 'string',
            sex: 'int',
            phone: 'string',
            id_card_num: 'string',
            aged_pic: 'string',
            citizen_card_pic: 'string',
            community_id: 'string',
            subdistrict_id: 'string'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.aged.main.editAged(this.ctx.request.body);
        return '编辑成功'
    }

    //老人管理查看统计
    @HttpGet('/:id/statistic')
    @ExceptionHandler()
    async viewStatistic() {
        const rule = {
            id: 'int'
        };
        this.validate(rule, this.ctx.params);
        this.result.data = await this.service.admin.aged.main.viewStatistic(this.ctx.params);
        return '查看成功'
    }


}

module.exports = AgedCtrl;
